package com.hq.fytj.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.eredlab.g4.arm.util.ArmConstants;
import org.eredlab.g4.ccl.datastructure.Dto;
import org.eredlab.g4.ccl.datastructure.impl.BaseDto;
import org.eredlab.g4.ccl.json.JsonHelper;
import org.eredlab.g4.ccl.util.G4Constants;
import org.eredlab.g4.rif.report.excel.ExcelExporter;
import org.eredlab.g4.rif.web.BaseAction;
import org.eredlab.g4.rif.web.CommonActionForm;

import com.hq.fytj.service.FytjService;

public class FytjAction extends BaseAction {
	private FytjService fytjservice=(FytjService)this.getService("fytjService");
	static private final String NOTTONGJI="--";
	//费用统计初始化
	public ActionForward singleInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("daochu");
	}
	//获得可以添加的费用类型
	public  ActionForward queryMoneyType(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		List list=this.g4Reader.queryForList("fytjDemo.queryMoneyType");
		this.write(JsonHelper.encodeList2PageJson(list, list.size(),null), response);
		return null;
	}
	//统计
	public  ActionForward find(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("正在查询数据！");
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println(dto);
		System.out.println("开始时间："+dto.getAsString("startTime"));
		System.out.println("结束时间："+dto.getAsString("endTime"));
		//每次查询都将查询条件保存起来！
		this.setSessionAttribute(request,"querydto",dto);
		dto.put("startTime", dto.getAsDate("startTime"));
		dto.put("endTime",dto.getAsDate("endTime"));
		List list=this.g4Reader.queryForList("fytjDemo.querySingle",dto);
		System.out.println("======================="+list+"\t\t\t共"+list.size()+"条记录！");
		for(int i=0;i<list.size();i++)
		{
			Dto curr=(Dto)(list.get(i));
			if(curr.get("bud_money")==null)
			{
				curr.put("bud_money",0);
			}
			if(curr.get("final_money")==null)
			{
				curr.put("final_money",0);
			}
			if(curr.get("detail_money")==null)
			{
				curr.put("detail_money",0);
			}
			if(curr.get("wfk")==null)
			{
				curr.put("wfk",curr.getAsBigDecimal("final_money").floatValue()-curr.getAsBigDecimal("detail_money").floatValue());
			}
			if(curr.get("zjqfk")==null)
			{
				curr.put("zjqfk",0);
			}
			curr.put("yfk", 0);
			curr.put("yfkbl",0);
			curr.put("zjqfbl",0);
			System.out.println("rrrrrrrrrrrrrr---"+curr.get("final_money"));
		}
		Integer count=(Integer)this.g4Reader.queryForObject("fytjDemo.querySingleCount", dto);
		System.out.println(count);
		this.write(JsonHelper.encodeList2PageJson(list,count,null), response);
		return null;
	}
	//统计
	public  ActionForward queryByMoneyType(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("正在统计费用！");
		//Thread.sleep(1000);
		CommonActionForm fForm = (CommonActionForm) form;
		//得到当前会话中之前的查询条件！
		Dto dto = (BaseDto) super.getSessionAttribute(request, "querydto");
		System.out.println("统计是的数据："+dto);
		System.out.println(dto);
		System.out.println("开始时间："+dto.getAsString("startTime"));
		System.out.println("结束时间："+dto.getAsString("endTime"));
		Dto param=new BaseDto();
		param.put("reportTitle","单向费用类型统计");
		param.put("name", this.g4Reader.queryForObject("fytjDemo.queryName",dto.getAsString("moneytype")));
		param.put("jiange",dto.getAsString("startTime")+" 到 "+dto.getAsString("endTime"));
		//记录集合！
		List list=this.g4Reader.queryForList("fytjDemo.querySingle",dto);
		float total_ys=0.00f,total_js=0.00f,total_yfbl=0.00f,total_yfk=0.00f,total_yfks=0.00f,total_wfk=0.00f,total_qfk=0.00f,total_qfbl=0.00f;
		//过滤数据，放置后面计算式出现异常！
		for(int i=0;i<list.size();i++)
		{
			Dto curr=(Dto)(list.get(i));
			if(curr.get("bud_money")==null)
			{
				curr.put("bud_money",0);
			}
			if(curr.get("final_money")==null)
			{
				curr.put("final_money",0);
			}
			if(curr.get("detail_money")==null)
			{
				curr.put("detail_money",0);
			}
			if(curr.get("wfk")==null)
			{
				curr.put("wfk",curr.getAsBigDecimal("final_money").floatValue()-curr.getAsBigDecimal("detail_money").floatValue());
			}
			curr.put("zjqfk",0);
			curr.put("yfk", 0);
			curr.put("yfkbl",0);
			curr.put("zjqfbl",0);
			total_ys+=curr.getAsBigDecimal("bud_money").floatValue();
			total_js+=curr.getAsBigDecimal("final_money").floatValue();
			total_yfbl+=curr.getAsBigDecimal("yfkbl").floatValue();
			total_yfk+=curr.getAsBigDecimal("yfk").floatValue();
			total_yfks+=curr.getAsBigDecimal("detail_money").floatValue();
			total_wfk+=curr.getAsBigDecimal("wfk").floatValue();
			total_qfk+=curr.getAsBigDecimal("zjqfk").floatValue();
			total_qfbl+=curr.getAsInteger("zjqfbl").floatValue();
		}
		//手动计算其他统计变量1
		param.put("total_ys",total_ys);
		param.put("total_js",total_js);
		param.put("total_yfbl",total_yfbl);
		param.put("total_yfk",total_yfk);
		param.put("total_yfks",total_yfks);
		param.put("total_wfk",total_wfk);
		param.put("total_qfk",total_qfk);
		param.put("total_qfbl",total_qfbl);
		param.put("countXmid", new Integer(list.size()));// 合计条数
		ExcelExporter excelExporter = new ExcelExporter();
		excelExporter.setTemplatePath("/fytj/fytj1.xls");
		excelExporter.setData(param, list);
		excelExporter.setFilename("单项费用类型统计报表.xls");
		excelExporter.export(request, response);
		return mapping.findForward(null);
	}
	//项目基本信息页面
	public ActionForward tongJi(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("tongji");
	}
	//费用统计初始化
	public ActionForward doubleTongJiInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("正在转向多项目统计页面。。。。。。。。。。");
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto=this.getPraramsAsDto(request);
		dto.put("typesin", dto.getAsString("types").replace(",", "\',\'"));
		dto.put("prj_sns",dto.getAsString("prj_sns").replace(",", "\',\'"));
		request.setAttribute("kaishi",dto.getAsString("startT"));
		request.setAttribute("jieshu",dto.getAsString("endT"));
		//供统计使用！
		this.setSessionAttribute(request, "dto",dto);
		return mapping.findForward("daoru");
	}
	
	//多费用多项目统计
	public  ActionForward total2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("正在进行多项目多费用统计！");
		CommonActionForm fForm = (CommonActionForm) form;
		Dto req=this.getPraramsAsDto(request);
		Dto dto=(Dto)this.getSessionAttribute(request, "dto");
		dto.put("start",req.getAsInteger("start"));
		dto.put("limit",req.getAsInteger("limit"));
		//this.removeSessionAttribute(request, "dto");
		System.out.println("传递过来的项目编号有："+dto.getAsString("prj_sns"));
		System.out.println("传递过来的费用编号有："+dto.getAsString("types"));
		System.out.println("开始时间："+dto.getAsDate("startT"));
		System.out.println("结束时间："+dto.getAsDate("endT"));
		dto.put("startT",dto.getAsDate("startT"));
		dto.put("endT",dto.getAsDate("endT"));
		dto.put("prj_sns",dto.getAsString("prj_sns"));
		//首先得到要查询项目的基本信息！
		List prjList=this.g4Reader.queryForPage("fytjDemo.selecPrj",dto);
		System.out.println("提示：共要统计"+prjList.size()+"条项目！");
		//然后查询所有的可用的费用类型
		List typeList=this.g4Reader.queryForList("fytjDemo.queryMoneyType");
		System.out.println("提示：总共有"+typeList.size()+"个费用类型！");
		String[] tjType=dto.getAsString("types").split(",");
		for(int i=0;i<tjType.length;i++)
		{
			System.out.println("要统计的费用有："+tjType[i]);
		}
		System.out.println("拆分后的要统计的费用数组大小为："+tjType.length);
		//等于0说明没有选择任何费用！
		if(tjType.length==0)
		{
			System.out.println("提示：不统计任何费用！");
			for(int i=0;i<prjList.size();i++)
			{
				Dto currDto=((Dto)prjList.get(i));
				for(int j=0;j<typeList.size();j++)
				{
					//当不统计任何费用时，用指定的符号代替！
					currDto.put("A"+((Dto)typeList.get(j)).getAsString("tp_cd"),NOTTONGJI);
				}
			}
		}
		else
		{
			System.out.println("提示：需要统计！");
			//将那些要统计的费用移除
			for(int i=0;i<tjType.length;i++)
			{
				for(int j=0;j<typeList.size();j++)
				{
					Dto d=(Dto)typeList.get(j);
					if(d.getAsString("tp_cd").equals(tjType[i]))
					{
						typeList.remove(d);
						break;
					}
				}
			}
			System.out.println("还剩多少个费用类型呢？"+typeList.size());
			//依次统计当前项目制定费用类型
			for(int i=0;i<prjList.size();i++)
			{
				Dto currDto=((Dto)prjList.get(i));
				System.out.println("提示：正在统计项目编号为"+currDto.getAsString("prj_sn")+"的费用，费用类型有："+tjType.toString());
				currDto.put("types",dto.getAsString("types"));
				currDto.put("startT",dto.getAsString("startT"));
				currDto.put("endT",dto.getAsString("endT"));
				currDto.put("typesin",dto.getAsString("typesin"));
				System.out.println(dto.getAsString("typesin"));
				//统计的如果有值就为具体值没有值就用0带填充，对于其他没有统计的费用用NOTTONGJI的值代替！
				List prjMoney=this.g4Reader.queryForList("fytjDemo.tongjiType",currDto);
				System.out.println("提示：统计单个项目的费用完毕！");
				//如果统计出来的数据个数同费用类型个数相同，说明都有数据！
				if(prjMoney.size()==tjType.length)
				{
					for(int k=0;k<prjMoney.size();k++)
					{
						currDto.put("A"+((Dto)prjMoney.get(k)).getAsString("derail_tp"),((Dto)prjMoney.get(k)).getAsString("detail_money"));
					}
					for(int l=0;l<typeList.size();l++)
					{
						//当不统计任何费用时，用指定的符号代替！
						currDto.put("A"+((Dto)typeList.get(l)).getAsString("tp_cd"),NOTTONGJI);
					}
				}
				//否则可能没有要查的数据或者只有部分费用类型有数据！
				else
				{
					//如果没有统计出任何数据，默认的值为0;
					if(prjMoney.size()==0)
					{
						System.out.println("没有统计到数据！");
						for(int m=0;m<tjType.length;m++)
						{
							currDto.put("A"+tjType[m],0);
						}
						for(int l=0;l<typeList.size();l++)
						{
							//当不统计任何费用时，用指定的符号代替！
							currDto.put("A"+((Dto)typeList.get(l)).getAsString("tp_cd"),NOTTONGJI);
						}
					}
					//如果只查出来部分数据
					else
					{
						System.out.println("只统计到了"+prjMoney.size()+"条数据！");
						//无论查到多少先封住数据值！
						for(int k=0;k<prjMoney.size();k++)
						{
							currDto.put("A"+((Dto)prjMoney.get(k)).getAsString("derail_tp"),((Dto)prjMoney.get(k)).getAsString("detail_money"));
						}
						//过滤一遍查看那些要统计的费用类型没有则说明没有查到！
						for(int n=0;n<tjType.length;n++)
						{
							if(currDto.get("A"+tjType[n])==null)
							{
								System.out.println("正在替换数据！");
								currDto.put("A"+tjType[n],0);
							}
						}
						//那那些不用统计的数据用指定的符号显示！
						for(int l=0;l<typeList.size();l++)
						{
							//当不统计任何费用时，用指定的符号代替！
							currDto.put("A"+((Dto)typeList.get(l)).getAsString("tp_cd"),NOTTONGJI);
						}
					}
				}
			}
		}
		Integer count=(Integer)this.g4Reader.queryForObject("fytjDemo.selecPrjCount", dto);
		this.write(JsonHelper.encodeList2PageJson(prjList,count,null), response);
		//用于后面生成excel表格！
		this.setSessionAttribute(request, "fList",prjList);
		return null;
	}
	
	//统计2
	public  ActionForward exportExce(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("正在生成多费用多项目excel表格！");
		Dto dto=(Dto)this.getSessionAttribute(request, "dto");
		Dto param=new BaseDto();
		param.put("reportTitle","多费用多项目统计");
		param.put("depart", this.getSessionContainer(request).getUserInfo().getUsername());
		param.put("startToEnd",dto.getAsString("startT")+" 到 "+dto.getAsString("endT"));
		//记录集合！
		List list=(List)this.getSessionAttribute(request, "fList");
		float total_a1=0.00f,total_a2=0.00f,total_a3=0.00f,total_a4=0.00f,total_a5=0.00f,total_a6=0.00f,total_a7=0.00f,total_a8=0.00f,total_a9=0.00f,total_a10=0.00f,total_a11=0.00f;
		//过滤数据，放置后面计算式出现异常！
		for(int i=0;i<list.size();i++)
		{
			Dto curr=(Dto)(list.get(i));
			for(int j=1;j<=11;j++)
			{
				if(curr.getAsString("A010201"+(j<=9?"0"+j:j)).equals("--"))
				{
					curr.put("A010201"+(j<=9?"0"+j:j),0);
				}
			}
			total_a1+=curr.getAsBigDecimal("A01020101").floatValue();
			total_a2+=curr.getAsBigDecimal("A01020102").floatValue();
			total_a3+=curr.getAsBigDecimal("A01020103").floatValue();
			total_a4+=curr.getAsBigDecimal("A01020104").floatValue();
			total_a5+=curr.getAsBigDecimal("A01020105").floatValue();
			total_a6+=curr.getAsBigDecimal("A01020106").floatValue();
			total_a7+=curr.getAsBigDecimal("A01020107").floatValue();
			total_a8+=curr.getAsBigDecimal("A01020108").floatValue();
			total_a9+=curr.getAsBigDecimal("A01020109").floatValue();
			total_a10+=curr.getAsBigDecimal("A01020110").floatValue();
			total_a11+=curr.getAsBigDecimal("A01020111").floatValue();
		}
		//手动计算其他统计变量1
		param.put("total_a1",total_a1);
		param.put("total_a2",total_a2);
		param.put("total_a3",total_a3);
		param.put("total_a4",total_a4);
		param.put("total_a5",total_a5);
		param.put("total_a6",total_a6);
		param.put("total_a7",total_a7);
		param.put("total_a8",total_a8);
		param.put("total_a9",total_a9);
		param.put("total_a10",total_a10);
		param.put("total_a11",total_a11);
		param.put("countXmid", new Integer(list.size()));// 合计条数
		ExcelExporter excelExporter = new ExcelExporter();
		excelExporter.setTemplatePath("/fytj/double.xls");
		excelExporter.setData(param, list);
		excelExporter.setFilename("多项目多费用类型统计报表.xls");
		excelExporter.export(request, response);
		return mapping.findForward(null);
	}
	//获得费用类型树
	public  ActionForward queryMoneyTypeTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("正在加载费用类型下的所有子节点。。。。。。。。。。。。父节点编号是："+request.getParameter("node"));
		//的到传来的父节点编号，至今我还不知道这个参数是从那里来的
		CommonActionForm aForm = (CommonActionForm)form;
		String pid=request.getParameter("node");
		//String pid = aForm.getTreeNodeUID4Clicked(request);
		Dto queryChild=new BaseDto();
		queryChild.put("pid",pid);
		List list=this.g4Reader.queryForList("fytjDemo.queryChildByAdmin",queryChild);
		//Dto menuDto = new BaseDto();
		for (int i = 0; i < list.size(); i++) 
		{
			Dto menuDto = (BaseDto) list.get(i);
			menuDto.put("checked", new Boolean(false));
			if (menuDto.getAsString("leaf").equals(ArmConstants.LEAF_Y))
				menuDto.put("leaf", new Boolean(true));
			else
				menuDto.put("leaf", new Boolean(false));
			if(menuDto.getAsString("id").length()==4)
			{
				menuDto.put("expanded",new Boolean(true));
			}
		}
		//将集合解析成json类型!
		write(JsonHelper.encodeObject2Json(list), response);
		return null;
	}
	//获得区域编号和名称
	public  ActionForward queryPrjAreas(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：正在加载区域类型。。。。"+dto);
		List list=this.g4Reader.queryForList("fytjDemo.queryPrjAreas");
		System.out.println("========================="+dto.getAsString("type"));
		if(dto.getAsString("type").trim().equals("all"))
		{
			//添加一个全部状态的选项
			Dto empty=new BaseDto();
			empty.put("tp_cd","");
			empty.put("tp_name", "全部");
			list.add(empty);
		}
		this.write(JsonHelper.encodeList2PageJson(list, list.size(),null), response);
		return null;
	}
	//获得区域编号和名称
	public  ActionForward queryPrjClasses(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println("提示：正在加载项目。。。。"+dto);
		List list=this.g4Reader.queryForList("fytjDemo.queryPrjClasses");
		if(dto.getAsString("type").trim().equals("all"))
		{
			//添加一个全部状态的选项
			Dto empty=new BaseDto();
			empty.put("tp_cd","");
			empty.put("tp_name", "全部");
			list.add(empty);
		}
		this.write(JsonHelper.encodeList2PageJson(list, list.size(),null), response);
		return null;
	}
	/**
	 * 查询项目
	 * 
	 */
	public ActionForward querySfxm(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("提示：正在查询项目基本信息。。。。。。。");
		CommonActionForm fForm = (CommonActionForm) form;
		Dto dto = fForm.getParamAsDto(request);
		System.out.println(dto);
		List list = g4Reader.queryForPage("fytjDemo.queryXmxx", dto);
		for(int i=0;i<list.size();i++)
		{
			Dto curr=(Dto)list.get(i);
			curr.put("areaname",(String)this.g4Reader.queryForObject("fytjDemo.queryTpCdName",curr.getAsString("prj_area")));
			curr.put("classname",(String)this.g4Reader.queryForObject("fytjDemo.queryTpCdName",curr.getAsString("prj_class")));
		}
		String jsonString = JsonHelper.encodeList2PageJson(list,  (Integer)this.g4Reader.queryForObject("fytjDemo.queryXmxxCount"), G4Constants.FORMAT_Date);
		super.write(jsonString, response);
		return mapping.findForward(null);
	}
}
